rm(list=ls())
library(tidyverse)

# Function to create datase with observations per muncode-year and time-variable columns 
center <-function(df,year_center){
  df <- df %>% 
    mutate(center = year - year_center) %>% 
    select(-year) %>%
    filter(between(center,-3,4)) %>% 
    mutate(center = str_replace(ifelse(center<0,paste0("t_minus",center),paste0("t_plus",center)),"-",""),
           center = str_replace(center,"t_plus0","t_0")) %>% 
    gather(variable, value, -c(mun_code,center)) %>%  
    unite(center,variable,center) %>%
    spread(center,value) %>% 
    mutate(year = year_center) 
  return(df)
}

# Read all required data -----

finbra <- read_rds(here::here("data","processed","fiscalpolicy","finbra_panel.rds"))

constitutional_transfers <- read_rds(here::here("data","processed","fiscalpolicy","panel_consitutional_grants.rds"))

gdp_mun <- read_rds(here::here("data","processed","citycharacteristics","gdp_mun.rds"))

mun_pop <- read_rds(here::here("data","processed","citycharacteristics","pop_mun.rds")) %>% 
  select(-mun_name) %>% 
  filter(year <= 2016)

gdp_deflator <- read_rds(here::here("data","processed","gdp_deflator.rds")) 
  
# Clean up gdp_mun -----
gdp_mun %>%
  filter(mun_gdp<0) %>% 
  pull(mun_code) # mun_code = 240450, mun_name = Guamare RN

gdp_mun <- gdp_mun %>% 
  filter(mun_gdp>0)

# Adjust unit from thousands reais to reais
gdp_mun <- gdp_mun %>% 
  mutate(mun_gdp = mun_gdp*1000) %>% # adjust units
  select(-gdp_unit)

# Create Finbra Outcomes ----

# Finbra clean up: change to missing all variables for mun in which capital_exp or current_exp or tot_funs == 0
finbra <- finbra %>% 
  mutate_at(vars(tot_rev:other_p),
            .funs = funs(ifelse((tot_rev == 0 | capital_exp == 0 | current_exp == 0 | tot_funs == 0),
                                NA,
                                .)))

# Compute Fiscal Variable Share of GDP 
finbra_share_gdp <- finbra %>% 
  left_join(gdp_mun, by = c("mun_code","year")) %>%
  select(-mun_name)

finbra_share_gdp <- finbra_share_gdp %>%
  mutate_at(vars(tot_rev:other_p),
            .funs = funs(sgdp = (./mun_gdp*100))) %>%
  select_at(vars(mun_code,year,ends_with("sgdp")))

# Compute Fiscal Variable at Constant Prices (measured in thousands reais of 2016)
finbra_constant_prices <- finbra %>% 
  left_join(gdp_deflator, by = "year") %>%
  mutate_at(vars(tot_rev:other_p),
            .funs = funs(cp = (./gdp_deflator/1000))) %>%
  select_at(vars(mun_code,year,ends_with("cp")))

# Compute Fiscal Variable Constant Prices Per Capita
finbra_per_capita <- finbra_constant_prices %>% 
  left_join(mun_pop, by = c("mun_code","year")) %>%
  mutate_at(vars(matches("cp")),
            .funs = funs(pc = ./mun_pop*1000)) %>% 
  rename_at(vars(matches("cp_pc")),
            .funs = funs(str_replace(.,"cp_pc","pc"))) %>%
  select_at(vars(mun_code,year,ends_with("pc")))

# Compute Expenditure Function Type as share Total Functional Expenditure
finbra_share <- finbra %>% 
  mutate_at(vars(tot_funs:other_p),
            .funs = funs(share = (./tot_funs)*100)) %>% 
  mutate_at(vars(tot_exp:investments),
            .funs = funs(share = (./tot_exp)*100)) %>%
  mutate_at(vars(tot_rev:fed_transf),
            .funs = funs(share = (./tot_rev)*100)) %>%
  select_at(vars(mun_code,year,ends_with("share")))

# Check if social + nonsocial sum to 1
finbra_share %>% 
  mutate(check_sum = social_exp_share+nonsocial_exp_share) %>% 
  filter(!near(check_sum,100)) %>% 
  nrow() # OK Pass test

# Check if social and nonsocial components sum to group total
finbra_share %>% 
  mutate(check_sum_social = education_share + health_share + welfare_share - social_exp_share) %>% 
  filter(!near(check_sum_social,0)) %>% 
  nrow() # OK Pass test

finbra_share %>% 
  mutate(check_sum_nonsocial = transport_share + housing_share + other_share - nonsocial_exp_share) %>% 
  filter(!near(check_sum_nonsocial,0)) %>% 
  nrow() # OK Pass test

# Create royalties ----

# Keep only oil royalties that create oil-windfalls
oil_royalties <- constitutional_transfers %>% 
  filter(transfer_type %in% c("Royalties - ANP","	Royalties - PEA")) %>% 
  select(mun_code = mun_code_ibge,year,total) %>% 
  group_by(mun_code,year) %>%
  summarise(oil_royalties = sum(total, na.rm =T)) %>% 
  ungroup() %>%
  filter(year <= 2016)

# Create mineral royalties
mineral_royalties <- constitutional_transfers %>% 
  filter(transfer_type %in% c("Royalties - CFM")) %>% 
  select(mun_code = mun_code_ibge,year,total) %>% 
  group_by(mun_code,year) %>%
  summarise(mineral_royalties = sum(total, na.rm =T)) %>% 
  ungroup() %>%
  filter(year <= 2016)

# Compute Fiscal Variable at Constant Prices (measured in thousands reais of 2016)
royalties_constant_prices <- oil_royalties %>% 
  full_join(mineral_royalties, by = c("year","mun_code")) %>% 
  left_join(gdp_deflator, by = "year") %>% 
  mutate(oil_royalties_cp = oil_royalties/gdp_deflator/1000,
         mineral_royalties_cp = mineral_royalties/gdp_deflator/1000) %>% 
  select_at(vars(mun_code,year,ends_with("cp"))) 

# Create FPM from constitutional grants dataset ----

fpm <- constitutional_transfers %>% 
  filter(transfer_type %in% c("FPM")) %>% 
  select(mun_code = mun_code_ibge,year,total) %>% 
  group_by(mun_code,year) %>%
  summarise(fpm_const_grant = sum(total, na.rm =T)) %>% 
  ungroup() %>%
  filter(year <= 2016)

# Compute Fiscal Variable at Constant Prices (measured in thousands reais of 2016)
fpm_constant_prices <- fpm %>% 
  left_join(gdp_deflator, by = "year") %>% 
  mutate(fpm_const_grant_cp = fpm_const_grant/gdp_deflator/1000) %>% 
  select_at(vars(mun_code,year,ends_with("cp"))) 

# Center fiscal variables ------------- 
fiscal_outcomes <- finbra_constant_prices %>%
  left_join(finbra_per_capita, by = c("mun_code","year")) %>%
  left_join(finbra_share_gdp, by = c("mun_code","year")) %>%
  left_join(finbra_share, by = c("mun_code","year")) %>%
  left_join(royalties_constant_prices, by = c("mun_code","year")) %>% 
  left_join(fpm_constant_prices, by = c("mun_code","year")) %>% 
  full_join(mun_pop, by = c("mun_code","year"))

# Replace NA to zero in the case of royalties
fiscal_outcomes <- fiscal_outcomes %>% 
  mutate(across(contains("royalties"),
         ~ifelse(is.na(.),0,.)))

# Center finbra outcomes
fiscal_outcomes_centered <- map(seq(1996,2012, by = 4), center, df = fiscal_outcomes) %>%
  bind_rows() %>%
  select(mun_code,year, everything()) %>%  
  mutate_at(vars(matches("_t_")),
            .funs = funs(as.numeric(.)))

# Check number of mun per election
fiscal_outcomes_centered %>%
  group_by(year) %>%
  summarise(n_obs = n())

# Save rds
write_rds(fiscal_outcomes_centered,here::here("data","processed","fiscalpolicy","fiscal_outcomes_centered.rds"))
